package com.item.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.Page;
import com.base.dialect.PaginationSupport;
import com.base.util.StringUtil;
import com.item.dao.model.News;
import com.item.dao.model.NewsExample;
import com.item.dao.model.NewsExample.Criteria;
import com.item.exception.JSONRepUtils;
import com.item.service.NewsService;
import com.item.util.WebPageFileController;

@Controller
public class NewsController {
	@Autowired
	private NewsService newsService;
	
	@RequestMapping("/listNews")
	@ResponseBody
	public String list(Page param,News news) throws Exception{
		NewsExample example = new NewsExample();
		PaginationSupport.setContext(param.getPageStart(), param.getPageSize(), null);
		Criteria criteria = example.createCriteria();
		//以下两个字段用来做筛选  分别是title和source的模糊匹配
		if(StringUtils.isNotBlank(news.getTitle())){
			criteria.andTitleLike("%"+news.getTitle()+"%");
		}
		if(StringUtils.isNotBlank(news.getDescription())){
			criteria.andTitleLike("%"+news.getDescription()+"%");
		}
		if(StringUtils.isNotBlank(news.getSource())){
			criteria.andSourceLike("%"+news.getSource()+"%");
		}
		if(StringUtils.isNotBlank(news.getDate())){
			criteria.andDateEqualTo(news.getDate());
		}
		if(news.getType() != null){
			criteria.andTypeEqualTo(news.getType());
		}
		if(news.getShowType() != null){
			criteria.andShowTypeEqualTo(news.getShowType());
		}
		example.setOrderByClause("date DESC,show_type DESC,top DESC");
		List<News> list = newsService.selectByExample(example);
		return JSONRepUtils.serialize(new Page(PaginationSupport.getContext().getTotalCount(), list), 0);
	}
	
	@RequestMapping("/showlistNews")
	public String list() {
		return "news/newsList";
	}
	
	@RequestMapping("/toEditNews")
	public String toEdit()throws Exception{
		return "news/newsEdit";
	}
	
	@RequestMapping(value="/saveNews",method=RequestMethod.POST)
	@ResponseBody
	public String save(String contextReal,News news, Integer isTop, HttpSession session)throws Exception{
		WebPageFileController fileController = new WebPageFileController("ydd", "news", "utf8");
		news.setCreateTime(new Date());
		try{
			if(StringUtil.isEmpty(news.getId())){
				if(StringUtils.isNotBlank(contextReal)) {
					news.setTop(0);
					news.setNoteId("");
					news.setImgPath("");
					news.setBrowseCount(0);
					news.setCommentCount(0);
					news.setPraiseCount(0);
					news.setIsShow(1);
					news.setUrl(fileController.write(fileController.getHtmlContentUTF8(contextReal), "html"));
					newsService.insert(news);
				}	
			}else{
				if(!StringUtils.isBlank(contextReal)) {
					//覆盖原来的文件
					news.setUrl(fileController.writeByUrl(fileController.getHtmlContentUTF8(contextReal), news.getUrl()));
					newsService.updateByPrimaryKeySelective(news);
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			return "{\"success\":false}";
		}
		
		return "{\"success\":true}";
	}
	
	@RequestMapping(value="/findByIdNews")
	@ResponseBody
	public String findById(String id,HttpSession session, Integer isM)throws Exception{		
		News n = newsService.selectByPrimaryKey(id);
		return JSONRepUtils.serialize(n, isM);
	}
	
	@RequestMapping("/delNews")
	@ResponseBody
	public String del(String id,String url,HttpSession session)throws Exception{
		WebPageFileController fileController = new WebPageFileController();
		String[] ids = id.split(",");
		String[] urls = url.split(",");
		int len = ids.length;
		for(int i = 0; i < len; i++) {
			newsService.deleteByPrimaryKey(ids[i]);
			fileController.deleteFile(urls[i]);
		}
		return "{\"success\":true}";
	}
	
	@RequestMapping("/topSetNews")
	@ResponseBody
	public String topSet(String id,HttpSession session) throws Exception {
		News record = new News();
		int max = newsService.getMaxTopValue();
		record.setTop(max + 1);
		record.setId(id);
		int rows = newsService.updateByPrimaryKeySelective(record);
		if (rows > 0)
			return "{\"success\":true}";
		else
			return "{\"success\":false}";
	}
	
	@RequestMapping("/topDelNews")
	@ResponseBody
	public String topDel(String id,HttpSession session) throws Exception {
		String[] ids = id.split(",");
		int len = ids.length;
		for(int i = 0; i < len; i++) {
			News record = new News();
			record.setTop(0);
			record.setId(ids[i]);
			newsService.updateByPrimaryKeySelective(record);
		}
		return "{\"success\":true}";
	}
	
	@RequestMapping("/addNewsBrowseCount")
	@ResponseBody
	public String addNewsBrowseCount(String id,HttpSession session) throws Exception {
		if(StringUtils.isNotBlank(id)){
			News record = new News();
			record.setId(id);
			newsService.addBrowseCount(record);
		}
		return "{\"success\":true}";
	}
}

